package game.colortest;

import game.Abstract2DMap;
import game.AbstractGame;
import game.Point;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

public class ColorTest extends Abstract2DMap
{
    private static ColorTest instance;
    
    public synchronized static ColorTest getInstance()
    {
        if (instance == null)
        {
            instance = new ColorTest();
        }
        return instance;
    }
    
    private List<Integer> getUniqueColors(int amount) 
    {
        final int lowerLimit = 0x10;
        final int upperLimit = 0xE0;
        final int colorStep = (int) ((upperLimit - lowerLimit) / Math.pow(amount, 1f / 3));
        final List<Integer> colors = new ArrayList<Integer>(amount);
        for (int R = lowerLimit; R < upperLimit; R += colorStep)
        {
            for (int G = lowerLimit; G < upperLimit; G += colorStep)
            {
                for (int B = lowerLimit; B < upperLimit; B += colorStep)
                {
                    if (colors.size() >= amount)
                    { // The calculated step is not very precise, so this
                      // safeguard is appropriate
                        return colors;
                    }
                    else
                    {
                        int color = (R << 16) + (G << 8) + (B);
                        colors.add(color);
                    }
                }
            }
        }
        return colors;
    }
    
    @Override
    public void render(Graphics g, int cellSize)
    {
        Point maxCells = ColorTestRunner.getInstance().calcMaxSize();
        List<Integer> colors = getUniqueColors(maxCells.getX() * maxCells.getY());
        for (int i = 0; i < colors.size(); i++)
        {
            g.setColor(new Color(colors.get(i)));
            Point position = calculatePosition(i);
            g.fillRect((int) position.getWidth() * cellSize, (int) position.getHeight() * cellSize, cellSize, cellSize);
        }
    }

    @Override
    protected void restart()
    {
    }

    @Override
    protected void doNext()
    {
        // TODO Auto-generated method stub
        
    }
    
    
    
    public static class ColorTestRunner extends AbstractGame
    {
        private static ColorTestRunner instance;
        
        public synchronized static ColorTestRunner getInstance()
        {
            if (instance == null)
            {
                instance = new ColorTestRunner();
                ColorTest.getInstance().setSize(instance.calcMaxSize());
            }
            return instance;
        }
        
        protected ColorTestRunner()
        {
            super(ColorTest.getInstance());
        }
        public static void main(String[] args)
        {
             Thread t = new Thread(ColorTestRunner.getInstance(), "ColorTest");
             t.start();
        }
    }
}
